package com.cjet.nissan.tcp.codec


import io.netty.buffer.ByteBuf
import io.netty.buffer.Unpooled
import io.netty.channel.ChannelHandlerContext
import io.netty.handler.codec.MessageToByteEncoder

/**
 * 消息结束分隔符
 */
class Encoder(var mDelimiter: ByteArray) : MessageToByteEncoder<String?>() {

    override fun encode(ctx: ChannelHandlerContext?, msg: String?, out: ByteBuf?) {
        if (msg == null || msg.length == 0) {
            return
        }
        if (!msg.isNullOrBlank() && mDelimiter.size > 0 ){
            out?.writeBytes(Unpooled.wrappedBuffer(msg.toByteArray(charset=Charsets.UTF_8)))
            out?.writeBytes(mDelimiter)
        }

    }
}